#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
void SetNonBlock(int fd) {
    int fl = fcntl(fd, F_GETFL);  
     if (fl < 0) {                                     
       perror("fcntl");  return;
     }
   fcntl(fd, F_SETFL, fl | O_NONBLOCK);  //设置为非阻塞
}         
   
using namespace std;  
int main()         
{                                                          
    char buffer[1024];                                     
    SetNonBlock(0);                                        
    while (true)                                           
    {                                                                                                                            
        ssize_t s = read(0, buffer, sizeof(buffer) - 1);   
        if (s > 0)
        {                       
            buffer[s] = 0;
            write(1,buffer,strlen(buffer));
           printf("读取成功!");
       }
       else
       {
           if (errno == EAGAIN || errno == EWOULDBLOCK)
           {
               sleep(1);
               std::cout << "当前没有出错，仅仅底层数据没有就绪罢了..." << std::endl;
               continue;
           }
           if(errno == EINTR){
               std::cout << "读取被信号中断" << std::endl;
               continue;
           }
           std::cout << "read error: " << s << std::endl;
           break;
      }
  }
    return 0;
}


